<template>
  <div>
  <!-- 安全分析弹窗-->
  <el-dialog title="安全分析" :visible.sync="aqfxBox" append-to-body width="60%"
             :before-close="handleCloseChooseMeasure">
    <avue-crud :option="jaoption"
               :table-loading="jaloading"
               :data="jadata"
               :page.sync="japage"
               :before-open="jabeforeOpen"
               ref="jacrud"
               @search-change="jasearchChange"
               @search-reset="jasearchReset"
               @selection-change="jaselectionChange"
               @current-change="jacurrentChange"
               @size-change="jasizeChange"
               @refresh-change="jarefreshChange"
               @on-load="jaonLoad">
    </avue-crud>
    <!-- 按钮 -->
    <div class="choose_cm_footer">
      <el-button
        type="primary"
        size="small"
        @click="chooseControlMeasure()">
        确 认
      </el-button>
      <el-button
        size="small"
        @click="cancelChooseControlMeasure()">
        取 消
      </el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/xcgl/jsaitem";
  import {mapGetters} from "vuex";
  import {getToken} from "@/util/auth";

  export default {
    props:{
      aqfxBox:{default:false},

    },
    data() {
      return {
        jaform: {},
        jaquery: {},
        jaloading: true,
        jaselectionList: [],
        japage: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        jaoption: {
          menu:false,
          addBtn:false,
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "工作步骤",
              prop: "gzbz",
              align:'center',
              searchSpan:4,
              search:true,
              type:"select",
              dataType:'number',
              width:100,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=xcgl_gzbz",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入工作步骤",
                trigger: "blur"
              }]
            },
            {
              label: "作业类型",
              prop: "zylx",
              align:'center',
              searchSpan:4,
              search:true,
              type:"select",
              dataType:'number',
              width:100,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=xcgl_zylx",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入作业类型",
                trigger: "blur"
              }]
            },
            {
              label: "危险因素",
              prop: "wxys",
              align:'center',
              searchSpan:4,
              search:true,
              type: "textarea",
              maxlength:100,
              span:24,
              rules: [{
                required: true,
                message: "请输入危险因素",
                trigger: "blur"
              }]
            },
            {
              label: "导致的后果",
              prop: "dzdhg",
              align:'center',
              type: "textarea",
              maxlength:100,
              span:24,
              rules: [{
                required: true,
                message: "请输入导致的后果",
                trigger: "blur"
              }]
            },
            {
              label: "控制措施",
              prop: "kzcs",
              align:'center',
              searchSpan:4,
              search:true,
              type: "textarea",
              maxlength:100,
              span:24,
              rules: [{
                required: true,
                message: "请输入控制措施",
                trigger: "blur"
              }]
            },
          ]
        },
        jadata: []
      };
    },
    computed: {
      ids() {
        let ids = [];
        this.jaselectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      // 选择安全作业
      chooseControlMeasure() {
        if(this.jaselectionList.length === 0) {
          this.$message.warning("请选择安全作业!");
          return;
        }
        this.$emit('chooseControlMeasure', this.jaselectionList);
        this.jaselectionClear()
      },
      // 取消选择安全作业
      cancelChooseControlMeasure() {
        this.jaselectionClear()
        this.$emit('cancelChooseControlMeasure');
      },

      // 关闭选择安全作业弹窗
      handleCloseChooseMeasure(done) {
        this.jaselectionClear()
        done();
        this.$emit('handleCloseChooseMeasure');

      },

      jabeforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.jaform.id).then(res => {
            this.jaform = res.data.data;
          });
        }
        done();
      },
      jasearchReset() {
        this.jaquery = {};
        this.jaonLoad(this.japage);
      },
      jasearchChange(params, done) {
        this.jaquery = params;
        this.japage.currentPage = 1;
        this.jaonLoad(this.japage, params);
        done();
      },
      jaselectionChange(list) {
        this.jaselectionList = list;
      },
      jaselectionClear() {
        this.jaselectionList = [];
        this.$refs.jacrud.toggleSelection();
      },
      jacurrentChange(currentPage){
        this.japage.currentPage = currentPage;
      },
      jasizeChange(pageSize){
        this.japage.pageSize = pageSize;
      },
      jarefreshChange() {
        this.jaonLoad(this.japage, this.jaquery);
      },
      jaonLoad(page, params = {}) {
        this.jaloading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.jaquery)).then(res => {
          const data = res.data.data;
          this.japage.total = data.total;
          this.jadata = data.records;
          this.jaloading = false;
          this.jaselectionClear();
        });
      }
    }
  };
</script>

<style lang="scss" scpoed>
  .choose_cm_footer{
    text-align: center;
  }
</style>
